package com.juxing.sdk.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;

/**
 * Dao基础类
 * @author brochen
 * @mail mongibello@163.com
 * @created 2016年2月1日
 *
 */
public abstract class BaseDao {

	/*写session*/
	private SqlSession writeSqlSession;
	/*读session*/
	private SqlSession readSqlSession;
	
	public SqlSession getWriteSqlSession() {
		return writeSqlSession;
	}
	public void setWriteSqlSession(SqlSession writeSqlSession) {
		this.writeSqlSession = writeSqlSession;
	}
	public SqlSession getReadSqlSession() {
		return readSqlSession;
	}
	public void setReadSqlSession(SqlSession readSqlSession) {
		this.readSqlSession = readSqlSession;
	}
	
	public void setWriteSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		if(writeSqlSession ==null) {
			writeSqlSession = new SqlSessionTemplate(sqlSessionFactory);
		}
	}
	public void setReadSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		if(readSqlSession ==null) {
			readSqlSession = new SqlSessionTemplate(sqlSessionFactory);
		}
	}
	
	/**
	 * 插入
	 * @param statement
	 * @return
	 */
	protected int insert(String statement) {
		return this.getWriteSqlSession().insert(statement);
	}
	/**
	 * 插入
	 * @param statement
	 * @param param
	 * @return
	 */
	protected int insert(String statement, Object param) {
		return this.getWriteSqlSession().insert(statement, param);
	}
	/**
	 * 删除
	 * @param statement
	 * @return
	 */
	protected int delete(String statement) {
		return this.getWriteSqlSession().delete(statement);
	}
	/**
	 * 删除
	 * @param statement
	 * @param param
	 * @return
	 */
	protected int delete(String statement, Object param) {
		return this.getWriteSqlSession().delete(statement, param);
	}
	/**
	 * 更新
	 * @param statement
	 * @return
	 */
	protected int update(String statement) {
		return this.getWriteSqlSession().update(statement);
	}
	/**
	 * 更新
	 * @param statement
	 * @param param
	 * @return
	 */
	protected int update(String statement, Object param) {
		return this.getWriteSqlSession().update(statement, param);
	}
	/**
	 * 查询一个
	 * @param statement
	 * @return
	 */
	protected <T> T selectOne(String statement) {
		return this.getReadSqlSession().selectOne(statement);
	}
	/**
	 * 查询一个
	 * @param statement
	 * @param param
	 * @return
	 */
	protected <T> T selectOne(String statement, Object param) {
		return this.getReadSqlSession().selectOne(statement, param);
	}
	/**
	 * 查询列表
	 * @param statement
	 * @return
	 */
	protected <E> List<E> selectList(String statement) {
		return this.getReadSqlSession().selectList(statement);
	}
	/**
	 * 查询列表
	 * @param statement
	 * @param param
	 * @return
	 */
	protected <E> List<E> selectList(String statement, Object param) {
		return this.getReadSqlSession().selectList(statement, param);
	}
}
